package com.ecar.api.support.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateUtil {

	public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
	
	public static final String DATE_YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";

	public static final String DATE_FORMAT_SHORT = "yyyy-MM-dd";
	
	public static final String DATE_FORMAT_TIME = "HH:mm:ss";

	public static final String DATE_FORMAT_COMPACT = "yyyyMMdd";

	public static final String DATE_FORMAT_COMPACTFULL = "yyyyMMddHHmmss";

	public static final String DATE_FORMAT_FULL_MSEL = "yyyyMMddHHmmssSSSS";

	public static final String DATE_YEAR_MONTH = "yyyyMM";
	
	public static final String DATE_YEAR_MONTH_TEMP = "yyyy-MM";

	/**
	 * 获取系统当前日期
	 * 
	 * @return
	 */
	public static Date getCurrentDate() {
		return new Date();
	}

	/**
	 * 得到当前日期
	 * 
	 * @return String 当前日期 yyyy-MM-dd HH:mm:ss格式
	 */
	public static String getCurDateTime() {
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		// String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
		DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_FULL);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(now.getTime()));
	}

	/**
	 * 得到当前日期
	 * 
	 * @return String 当前日期 yyyyMMddHHmmss格式
	 */
	public static String getCurDateTime1() {
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_COMPACTFULL);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(now.getTime()));
	}

	/**
	 * 得到当前日期YYYYMM格式
	 * 
	 * @return String 当前日期 yyyyMM格式
	 */
	public static String getCurDateYYYYMM() {
		Calendar now = Calendar.getInstance(TimeZone.getDefault());

		DateFormat sdf = new SimpleDateFormat(DATE_YEAR_MONTH);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(now.getTime()));
	}

	/**
	 * 得到下个月日期YYYYMM格式
	 * 
	 * @return String 当前日期 yyyyMM格式
	 */
	public static String getNextMonthDateYYYYMM() {
		Calendar cal = Calendar.getInstance(TimeZone.getDefault());
		cal.add(Calendar.MONTH, 1);
		DateFormat sdf = new SimpleDateFormat(DATE_YEAR_MONTH);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(cal.getTime()));
	}

	/**
	 * 得到当前日期
	 * 
	 * @return String 当前日期 yyyyMMdd格式
	 */
	public static String getCurDateYYYYMMDD() {
		Calendar now = Calendar.getInstance(TimeZone.getDefault());

		DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_COMPACT);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(now.getTime()));
	}

	/**
	 * 判断否是今天
	 * 
	 * @param strDate  yyyy-MM-dd
	 * @return
	 */
	public static boolean isCurrentDay(String strDate) {
		boolean bRet = false;
		try {
			Calendar now = Calendar.getInstance(TimeZone.getDefault());
			// String DATE_FORMAT = "yyyy-MM-dd";
			DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SHORT);
			sdf.setTimeZone(TimeZone.getDefault());
			Date date1 = sdf.parse(strDate);
			String strDate1 = sdf.format(date1);
			String strDate2 = sdf.format(now.getTime());
			bRet = strDate1.equalsIgnoreCase(strDate2);
		} catch (ParseException e) {
			e.printStackTrace();

		}
		return bRet;
	}

	/**
	 * 判断否是今天
	 * 
	 * @param Date
	 *            
	 * @return
	 */
	public static boolean isCurrentDay(Date dt) {
		boolean bRet = false;
		Calendar now = Calendar.getInstance(TimeZone.getDefault());

		DateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SHORT);
		sdf.setTimeZone(TimeZone.getDefault());
		String strDate1 = sdf.format(dt);
		String strDate2 = sdf.format(now.getTime());
		bRet = strDate1.equalsIgnoreCase(strDate2);
		return bRet;
	}
	
	/**
	 * 判断是否今天某个小时
	 * @param dt
	 * @param hour
	 * @return
	 */
	public static boolean isCurrentDayHour(Date dt) {
		boolean bRet = false;
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		DateFormat sdf = new SimpleDateFormat(DATE_YEAR_MONTH_DAY_HOUR);
		sdf.setTimeZone(TimeZone.getDefault());
		String strDate1 = sdf.format(dt);
		String strDate2 = sdf.format(now.getTime());
		bRet = strDate1.equalsIgnoreCase(strDate2);
		return bRet;
	}
	
	/**
	 * 判断一个时间是否存在于一个时间区间内
	 * @param currentTime
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static boolean isTimeInterval(Date currentTime,Date startTime,Date endTime){
		return (currentTime.compareTo(startTime) >= 0 &&
	            currentTime.compareTo(endTime) <= 0);
	}
	/**
	 * 获取几小时后的时间
	 * 
	 * @param hour
	 *            小时
	 * @param format
	 *            hh:mm:ss
	 * @return HH:MM:SS
	 */
	public static String getAfterDateTime(int hour, String format) {
		long lTime = new Date().getTime() + hour * 60 * 60 * 1000;
		Calendar calendar = new GregorianCalendar();
		java.util.Date date_now = new java.util.Date(lTime);
		calendar.setTime(date_now);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		java.util.Date date = calendar.getTime();
		return sdf.format(date);
	}

	/**
	 * 获取几小时后的时间
	 * 
	 * @param hour
	 *            小时
	 * @param format
	 *            hh:mm:ss
	 * @return java.util.Date
	 */
	public static java.util.Date getAfterDateTime(long hour) {
		long lTime = new Date().getTime() + hour * 60 * 60 * 1000;
		Calendar calendar = new GregorianCalendar();
		java.util.Date date_now = new java.util.Date(lTime);
		calendar.setTime(date_now);
		java.util.Date date = calendar.getTime();
		return date;
	}

	/**
	 * 得到当前日期
	 * 
	 * @param format日期格式
	 * @return String 当前日期 format日期格式
	 */
	public static String getCurDateTime(String format) {
		try {
			Calendar now = Calendar.getInstance(TimeZone.getDefault());
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(now.getTime()));
		} catch (Exception e) {
			return getCurDateTime(); // 如果无法转化，则返回默认格式的时间。
		}
	}

	/**
	 * 得到时间戳
	 * 
	 * @param null
	 * @return String 当前日期时间戳(yyyyMMddHHmmssSSSS)
	 */
	public static String getTimeStamp() {
		try {
			Calendar now = Calendar.getInstance(TimeZone.getDefault());
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_FULL_MSEL);
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(now.getTime()));
		} catch (Exception e) {
			return getCurDateTime(); // 如果无法转化，则返回默认格式的时间。
		}
	}

	/**
	 * 日期转字符串
	 * 
	 * @return String
	 */
	public static String parseDateToString(Date thedate, String format) {
		DateFormat df = new SimpleDateFormat(format);
		if (thedate != null) {
			return df.format(thedate.getTime());
		}
		return "";
	}

	/**
	 * 日期转字符串 
	 * @param thedate
	 * @return
	 */
	public static String parseDateToString(Date thedate) {
		// String format = "yyyy-MM-dd";
		return parseDateToString(thedate, DATE_FORMAT_SHORT);
	}

	/**
	 * 字符串日期
	 * 
	 * @return Date
	 */
	public static Date parseStringToDate(String thedate, String format) {
		DateFormat sdf = new SimpleDateFormat(format);
		Date dd1 = null;
		try {
			if(thedate != null && !thedate.equals("")){
				dd1 = sdf.parse(thedate);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dd1;
	}

	/**
	 * 由String型日期转成format形式String
	 * 
	 * @param format1原先格式
	 * @param format2转化格式
	 * @return String
	 */
	public static String changeFormatDateString(String format1, String format2, String strDate) {
		if (strDate == null)
			return "";
		if (strDate.length() >= format1.length() && format1.length() >= format2.length()) {
			return parseDateToString(parseStringToDate(strDate, format1), format2);
		}
		return strDate;
	}

	// 得到当前日期的前一天时间 yyyymmdd
	public static String beforeNDaysDate(String format) {
		Date dNow = new Date(); // 当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		dBefore = calendar.getTime(); // 得到前一天的时间
		SimpleDateFormat sdf = new SimpleDateFormat(format); // 设置时间格式
		String timed = sdf.format(dBefore); // 格式化前一天
		return timed;
	}

	/**
	 *  获得N个月后的日期
	 *  
	 *  theDate 日期
	 *  
	 *  int month 月数
	 *  
	 *  format 格式
	 * 
	 */
	public static String afterNMonthDate(String theDate, int month ,String format){
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		Date dd1 = null;
		try {
			dd1 = sdf.parse(theDate);
			cal.setTime(dd1);
			cal.add(Calendar.MONTH, month);
			sdf.setTimeZone(TimeZone.getDefault());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (sdf.format(cal.getTime()));
	}
	
	/*
	 * 得到n天后的日期返回date类型
	*/
	public static Date afterNDaysDate(Date date, int nDayNum) {
		Calendar c=Calendar.getInstance();   
		c.setTime(date);   
		c.add(Calendar.DATE,nDayNum);   
		Date d2=c.getTime();   
		System.out.println(d2);
		return d2;   
	}
	
	public static  Date getdate(int i)
	{
		Date dat = null;
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, i);
		dat = cd.getTime();
		SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Timestamp date = Timestamp.valueOf(dformat.format(dat));
		return date;
	}
	
	/**
	 * 得到N天后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N天
	 * @return String N天后的日期
	 */
	public static String afterNDaysDate(String theDate, String nDayNum, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.DAY_OF_YEAR, Integer.parseInt(nDayNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(format); // 如果无法转化，则返回默认格式的时间。
		}
	}

	/**
	 * 得到N小时后的日期
	 * 
	 * @param theDate某日期
	 *            格式传入传出格式都是 format格式
	 * @param nDayNum
	 *            N小时
	 * @return String N小时后的日期
	 */
	public static String afterNHoursDate(String theDate, String nHourNum, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(nHourNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(format); // 如果无法转化，则返回默认格式的时间。
		}
	}
	
	/**
	 * 得到N小时后的日期
	 * 
	 * @param theDate某日期
	 *            格式传入传出格式都是 format格式
	 * @param nDayNum
	 *            N小时
	 * @return String N小时后的日期
	 */
	public static Date afterNHoursDate(String theDate, String nHourNum) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_FULL);
				Date dd1 = sdf.parse(theDate);
				Calendar cal = Calendar.getInstance();
				cal.setTime(dd1);
				cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(nHourNum));
				return new Date(cal.getTime().getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	}
	
	/**
	 * 得到N小时后的日期
	 * 
	 * @param Date dd1 时间
	 * @param nDayNum
	 *            N小时
	 * @return String N小时后的日期
	 */
	public static Date afterNHoursDate(Date dd1, String nHourNum) {
			try {
				Calendar cal = Calendar.getInstance();
				cal.setTime(dd1);
				cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(nHourNum));
				return new Date(cal.getTime().getTime());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
	}

	/**
	 * 得到N分钟后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static String afterNMinsDate(String theDate, String nMinNum, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.MINUTE, Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(format); // 如果无法转化，则返回默认格式的时间。
		}
	}
	
	/**
	 * 得到N分钟后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static String afterNMinsDate(Date date, String nMinNum) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_FULL);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MINUTE, Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(DateUtil.DATE_FORMAT_FULL); // 如果无法转化，则返回默认格式的时间。
		}
	}
	
	/**
	 * 得到N分钟后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static Date afterNMinsDateToDate(Date date, String nMinNum) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_FULL);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MINUTE, Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return cal.getTime();
		} catch (Exception e) {
			return null; // 如果无法转化，则返回默认格式的时间。
		}
	}
	
	/**
	 * 得到N分钟后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static Date afterNMinsToDate(Date date, String nMinNum) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FORMAT_FULL);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MINUTE, Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return cal.getTime();
		} catch (Exception e) {
			return new Date();
		}
	}
	
	/**
	 * 得到N分钟前的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static String beforeNMinsDate(String theDate, String nMinNum, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.MINUTE, -Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(format); // 如果无法转化，则返回默认格式的时间。
		}
	}
	
	/**
	 * 得到N分钟前的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static Date beforeNMinsDate(Date date, String nMinNum) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MINUTE, -Integer.parseInt(nMinNum));
			return new Date(cal.getTime().getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	/**
	 * 得到N分钟前的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static Date beforeNMinsDate(String theDate, String nMinNum) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_FULL);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.MINUTE, -Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return new Date(cal.getTime().getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到N秒后的日期
	 * 
	 * @param theDate某日期
	 *            格式 yyyy-MM-dd
	 * @param nDayNum
	 *            N分钟
	 * @return String N分钟后的日期
	 */
	public static String afterNSecondsDate(String theDate, String nMinNum, String format) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dd1 = sdf.parse(theDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(dd1);
			cal.add(Calendar.MILLISECOND, Integer.parseInt(nMinNum));
			sdf.setTimeZone(TimeZone.getDefault());
			return (sdf.format(cal.getTime()));
		} catch (Exception e) {
			return getCurDateTime(format); // 如果无法转化，则返回默认格式的时间。
		}
	}

	// 比较两个字符串格式日期大小,带格式的日期
	public static boolean isBefore(String strdat1, String strdat2, String format) {
		try {
			Date dat1 = parseStringToDate(strdat1, format);
			Date dat2 = parseStringToDate(strdat2, format);
			return dat1.before(dat2);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 比较两个字符串格式日期大小,带格式的日期,返回int
	public static long isBefore_int(String strdat1, String strdat2, String format) {
		long result = 0;
		try {
			Date dat1 = parseStringToDate(strdat1, format);
			Date dat2 = parseStringToDate(strdat2, format);
			return dat2.getTime() - dat1.getTime();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 比较两个字符串格式日期大小,默认转换格式:yyyy-MM-dd
	public static boolean isBefore(String strdat1, String strdat2) {
		// String format = "yyyy-MM-dd";
		return isBefore(strdat1, strdat2, DATE_FORMAT_SHORT);
	}
	
	// 比较日期是不是同一天
	public static boolean isSameDay(Date date1,Date date2) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SHORT);
		String datestr1 = sdf.format(date1);
		String datestr2 = sdf.format(date2);
		if(datestr1.equals(datestr2))
			return true;
		return false;
	}

	/**
	 * 获取休息时间
	 * 
	 * @param strTime
	 *            strTime=" 3:00:00"; 需要休息的时间，注意有空格
	 * @return 需要休息的时间
	 * @throws ParseException
	 */
	public static long getSleepTime(String strTime) throws ParseException {
		long p = 1;
		long l_date = System.currentTimeMillis();
		java.util.Date date_now = new java.util.Date(l_date);
		SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT_SHORT);
		String strDate = fmt.format(date_now) + strTime;
		SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_FULL);
		if (date_now.before(df.parse(strDate)))
			p = (df.parse(strDate)).getTime() - l_date;
		else {
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date_now);
			calendar.add(Calendar.DAY_OF_YEAR, 1);
			java.util.Date date = calendar.getTime();
			strDate = fmt.format(date) + strTime;
			p = (df.parse(strDate)).getTime() - l_date;
		}
		return p;
	}

	public static Timestamp getCurrentTime() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static String getHourMinute(String fullTime) {
		return fullTime.substring(11, 16);
	}

	// 得到日期数组中最小日期
	public static String getMinDateOfArray(String dateArray[]) {
		String dateTmp = "";
		if (dateArray != null) {
			dateTmp = dateArray[0];
			for (int i = 1; i < dateArray.length; i++) {
				if (isBefore(dateArray[i], dateTmp, DATE_FORMAT_SHORT)) {
					dateTmp = dateArray[i];
				}
			}
		}
		return dateTmp;
	}

	// 得到日期数组中最大日期
	public static String getMaxDateOfArray(String dateArray[]) {
		String dateTmp = "";
		if (dateArray != null) {
			dateTmp = dateArray[0];
			for (int i = 1; i < dateArray.length; i++) {
				if (isBefore(dateTmp, dateArray[i], DATE_FORMAT_SHORT)) {
					dateTmp = dateArray[i];
				}
			}
		}
		return dateTmp;
	}

	public static boolean hasNextDayInArray(String dateArray[], String dateTmp) {
		int index = 0;
		if (dateArray != null) {
			Arrays.sort(dateArray);
			for (int i = 0; i < dateArray.length; i++) {
				if (dateArray[i].equals(dateTmp)) {
					index = i;
				}
			}
			System.out.println(index);
			return index + 1 != dateArray.length;
		}
		return false;
	}

	public static boolean hasPreviousDayInArray(String dateArray[], String dateTmp) {
		int index = 0;
		if (dateArray != null) {
			Arrays.sort(dateArray);
			for (int i = 0; i < dateArray.length; i++) {
				if (dateArray[i].equals(dateTmp)) {
					index = i;
				}
			}
			return index != 0;
		}
		return false;
	}

	/*
	 * 得到上一个月或者下一个月的日期
	 */
	public static String getDayafterMonth(String theDate, int month, String formatStr) {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		Date dat1 = parseStringToDate(theDate, formatStr);
		now.setTime(dat1);
		sdf.setTimeZone(TimeZone.getDefault());
		now.add(Calendar.MONTH, month);
		return sdf.format(now.getTime());
	}
   
	/**
	 * <p>
	 * 获取当前系统时间的小时数
	 * </p>
	 * 
	 * @return
	 */
	public static int getCurrentHour() {
		Calendar calendar = new GregorianCalendar();
		return calendar.get(Calendar.HOUR_OF_DAY);
	}
	
	//根据时间戳获取小时数
	public static int getHourByTimeStamp(String timeStamp){
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		Long time = Long.valueOf(timeStamp);
		String curtime = sdf.format(time);
		return Integer.valueOf(curtime);
	}

	/**
	 * <p>
	 * 获取当前系统时间的分钟数
	 * </p>
	 * 
	 * @return
	 */
	public static int getCurrentMinutes() {
		Calendar calendar = new GregorianCalendar();
		return calendar.get(Calendar.MINUTE);
	}

	public static Date getFristDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}

	public static Date getLastDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		return calendar.getTime();
	}
	
	/**
	 * 得到N秒后的日期
	 * @param theDate
	 * @param nMinNum 秒
	 * @return
	 */
	public static Date afterNSecondsDateToDate(Date theDate, int nMinNum) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(theDate);
		cal.add(Calendar.MILLISECOND, nMinNum*1000);
		return cal.getTime();
	}
	
	/**
	 * 返回N年后的时间
	 * @param year
	 * @return
	 */
	public static Date getLastYEAR(int year) {
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, year);
        return calendar.getTime();
	}

	/**
	 * 将秒转换为小时分秒等
	 * 
	 * @param sec
	 * @return
	 */
	public static String changeTimetohm(int sec) {
		String temp = "";
		if (sec < 60) {
			temp = "00:00:" + sec + "";
		} else if (sec < 3600) {
			temp = "00:" + sec / 60 + ":" + sec % 60;
		} else {
			temp = "" + sec / 3600 + ":" + (sec % 3600) / 60 + ":" + sec % 60 + "";
		}
		return temp;
	}
	
	/**
	 * 将毫秒转换为小时分秒等
	 * 
	 * @param sec
	 * @return
	 */
	public static String changeTimetohm(long sec) {
		String temp = "";
		sec = sec / 1000;
		if (sec < 60) {
			temp = "00:00:" + sec + "";
		} else if (sec < 3600) {
			temp = "00:" + sec / 60 + ":" + sec % 60;
		} else {
			temp = "" + sec / 3600 + ":" + (sec % 3600) / 60 + ":" + sec % 60 + "";
		}
		return temp;
	}

	/**
	 * 将秒转换为小时分秒等
	 * 
	 * @param sec
	 * @return
	 */
	public static String changeTime(int sec) {
		String temp = "";
		if (sec < 60) {
			temp = "" + sec + "秒";
		} else if (sec < 3600) {
			temp = "" + sec / 60 + "分" + sec % 60 + "秒";
		} else {
			temp = "" + sec / 3600 + "小时" + (sec % 3600) / 60 + "分" + sec % 60 + "秒";
		}
		return temp;
	}
	
	/**
	 * 时间相减
	 * @param d1
	 * @param d2
	 * @return 毫秒
	 */
	public static long timeMinus(Date d1,Date d2) {
		Long ss = (d1.getTime() - d2.getTime());
		return ss;
	}
	
	/**
	 * 获得两个时间相减之后的倒计时
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static String getTimetohm(Date d1,Date d2) {
		long ss = DateUtil.timeMinus(d1, d2);
		if(ss <= 0)
			return "00:00:00";
		else
			return DateUtil.changeTimetohm(ss);
	}
	
	/**
	 * 传入小时,分钟,获取今天的时间
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static Date getTodayTime(String hour,String minute) {
		Calendar cal = Calendar.getInstance();
		if(hour != null && hour.trim().length() > 0)
			cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
		else
			cal.set(Calendar.HOUR_OF_DAY, 0);
		if(minute != null && minute.trim().length() > 0)
			cal.set(Calendar.MINUTE, Integer.parseInt(minute));
		else
			cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 传入小时,分钟,获取今天的时间
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static String getTodayTimeToString(String hour,String minute) {
		Date d = getTodayTime(hour, minute);
		SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT_FULL);
		return fmt.format(d);
	}
	
	public static String getTodayTimeToString(Date d) {
		SimpleDateFormat fmt = new SimpleDateFormat(DATE_FORMAT_FULL);
		return fmt.format(d);
	}
	
	public static String getDate(String year,String month,String day)
	{
		if(Integer.parseInt(month)<10)
		{
			month="0"+month;
		}
		if(Integer.parseInt(day)<10)
		{
			day="0"+day;
		}
		return year+"-"+month+"-"+day;
	}

	
	/**
	 * 输入当前日期、N天后.返回日期
	 * @param d
	 * @param n
	 * @return
	 */
	public static String afterNDay(Date d,int n){
		
		Calendar c=Calendar.getInstance();   
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd");   
		c.setTime(d);   
		c.add(Calendar.DATE,n);   
		Date d2=c.getTime();   
		String s=df.format(d2);
		
		return s;   
	}  
	/**
	 * 输入当前日期、N天后.返回日期
	 * @param d
	 * @param n
	 * @return
	 */
	public static String afterNDayTime(Date d,int n){
		
		Calendar c=Calendar.getInstance();   
		DateFormat df=new SimpleDateFormat(DATE_FORMAT_FULL);   
		c.setTime(d);   
		c.add(Calendar.DATE,n);   
		Date d2=c.getTime();   
		String s=df.format(d2);
		
		return s;   
	}  

	
	
	public static void main(String args[]) { 
		System.out.println(parseStringToDate("", DATE_FORMAT_FULL));
		/*String str = getTodayTimeToString("1",null);
		String str1 = getTodayTimeToString("23","59");
		System.out.println(str);
		System.out.println(str1);*/
		/*Date t = DateUtil.parseStringToDate("2012-12-20 10:30:12",
				DateUtil.DATE_FORMAT_FULL);
		System.out.println(t);DateUtil.getAfterDateTime(30*24)
		System.out.println(parseDateToString(t));*/
		//System.out.println(DateUtil.getAfterDateTime(2,"HH:mm:ss"));
		/*System.out.println(DateUtil.isBefore_int("2014-12-20", "2014-12-20", DATE_FORMAT_SHORT)/86400000);
		*//*Date d = DateUtil.parseStringToDate("2014-12-20 10:30:12", DATE_FORMAT_FULL);
		Date d1 = DateUtil.parseStringToDate("2014-12-10 10:30:12", DATE_FORMAT_FULL);
		Long n = d.getTime() - d1.getTime();
		System.out.println(DateUtil.changeTime(n.intValue()/1000));*/
		//System.out.println(DateUtil.getAfterDateTime(30*24).toLocaleString());
	/*	double s = 50/100.00*Double.parseDouble("5");
		long sysdabi = (long)Math.ceil(s);
		System.out.println(Math.ceil(sysdabi));*/
		/*Random r = new Random();
		r.nextInt();
		//System.out.println(DateUtil.isSameDay(DateUtil.parseStringToDate("2014-09-15 09:00:00", DATE_FORMAT_SHORT), DateUtil.getCurrentDate()));
		System.out.println(r.nextInt()%2);*/
	/*	try {
			System.out.println(CharsetUtil.encodeToUTF8("%E6%AD%A6%E6%B1%89%E5%B8%82"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}*/
		/*Date d1 = new Date();
		Date d2 = DateUtil.parseStringToDate("2014-09-28 13:40:00", DateUtil.DATE_FORMAT_FULL);
		System.out.println(DateUtil.timeMinus(d1, d2));
		System.out.println(changeTimetohm((int)(DateUtil.timeMinus(d1, d2)/1000)));
		System.out.println( changeTimetohm(DateUtil.timeMinus(d1, d2)%(30*60*1000)) );*/
		/*Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.HOUR, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
		cal1.set(Calendar.MILLISECOND, 0);
		
		Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR, 1);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		System.out.println(cal1.getTime());
		System.out.println(cal.getTime());
		
		
		System.out.println(isTimeInterval(new Date(), cal1.getTime(), cal.getTime()));
*/	
		/*Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		System.out.println(cal.getTime().toLocaleString());
		*/
		/*Date date = DateUtil.getTodayTime("20", null);
		System.out.println(date);
		System.out.println(DateUtil.timeMinus(date, new Date()));
		System.out.println(DateUtil.changeTimetohm(DateUtil.timeMinus(date, new Date())));*/
		//System.out.println(DateUtil.beforeNMinsDate("2014-11-21 00:00:00", "5"));
//		System.out.println(new Date().after(DateUtil.beforeNMinsDate("2014-11-21 00:00:00", "5")));
//		System.out.println(new Date().before(DateUtil.beforeNMinsDate("2014-11-21 00:00:00", "5")));
		/*System.out.println(new Date());
		System.out.println(DateUtil.afterNSecondsDateToDate(DateUtil.StrToDate("2014-11-21 00:00:00"), 50).toLocaleString());
		*/
		//System.out.println(DateUtil.isCurrentDayHour(new Date()));
		
//		Date d2 = DateUtil.parseStringToDate("2015-03-07 09:57:32", DateUtil.DATE_FORMAT_FULL);
//		System.out.println((DateUtil.timeMinus(new Date(),d2)-10000)/1000);
		
		System.out.println(DateUtil.formatToDHS(1432086166295l));
		System.out.println("本月有" +getDays(getYear(new Date()),getMonth(new Date())) + "天");  
		System.out.println(afterNDaysDate("2015-07-02","2","yyyy-MM-dd"));
		System.out.println(afterNMonthDate("2015-07-31", -1, "yyyy-MM-dd"));
		System.out.println(getMonth(new Date()));
	}
	
	/**
	 * 根据毫秒，返回时分 如 10:12
	 * @param l
	 * @return
	 */
	public String formatLongToTimeStr(Long l) {        
		int hour = 0;        
		int minute = 0;       
		int second = 0;        
		second = l.intValue() / 1000;        
		if (second >= 60) {            
			minute = second / 60;            
			second = second % 60;        
		}       
		if (minute > 60) {            
			hour = minute / 60;            
			minute = minute % 60;        
		}        
		String strtime = hour+":"+minute+":"+second;
		return strtime;   
	}
	
	/**
	 * 传入小时数,返回今天的时间
	 * @param str 12:00
	 * @return
	 */
	public static Date getNowDate(String time) throws Exception{
		Date d = new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String da=format.format(d);
		time = da + " " + time + ":00";
		return format2.parse(time);
	}
	
	/**
	 * 传入小时数,返回N天后的时间
	 * @param str 12:00
	 * @return
	 */
	public static Date getNowDate(String time, int day) throws Exception{
		Calendar ca = Calendar.getInstance();
		SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		ca.setTime(getNowDate(time));
		ca.set(Calendar.DATE, ca.get(Calendar.DATE) + day);	
		
		return format2.parse(format2.format(ca.getTime()));
	}
	
	/**
	* 字符串转换成日期
	* @param str
	* @return date
	*/
	public static Date StrToDate(String str) {
	  
	   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	   Date date = null;
	   try {
	    date = format.parse(str);
	   } catch (ParseException e) {
	    e.printStackTrace();
	   }
	   return date;
	}
	
	public static String formatToDHS(long ms) {//将毫秒数换算成x天x时x分x秒x毫秒
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;

		long day = ms / dd;
		long hour = (ms - day * dd) / hh;
		long minute = (ms - day * dd - hour * hh) / mi;
		long second = (ms - day * dd - hour * hh - minute * mi) / ss;
		long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

		String strDay = day < 10 ? "0" + day : "" + day;
		String strHour = hour < 10 ? "0" + hour : "" + hour;
		String strMinute = minute < 10 ? "0" + minute : "" + minute;
//		String strSecond = second < 10 ? "0" + second : "" + second;
		String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
		strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
		return strDay + ":" + strHour + ":" + strMinute;
	}
	
	/**
	 * 毫秒数转换成yyyy-MM-dd HH:mm:ss格式字符串
	 * @param MS
	 * @return
	 */
	public static String MSToDateStr(long MS){
		Date date = new Date(MS);
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_FULL);
		return sdf.format(date);
	}
	
	/**
	 * 时间转换成毫秒数字符串
	 * @param date
	 * @return
	 */
	public static String dateToMS(Date date){
		return String.valueOf(date.getTime());
	}
	
	/**
	 * 获取当前时间的时间，如10，11，12
	 * @return
	 */
	public static int  getCurrentDay(){
		Calendar ca = Calendar.getInstance();
	    return ca.get(Calendar.DATE);//获取日
	}
	
	
	/**
	 * 返回当前年份  
	 * @return
	 */
	public static  int getYear(Date date)  
    {  
        String year = new SimpleDateFormat("yyyy").format(date);  
        return Integer.parseInt(year);  
    }  
  
    /**
     * 返回指定日期当前月份  
     * @return
     */
	public static  int getMonth(Date date)  
    {  
        String month = new SimpleDateFormat("MM").format(date);  
        return Integer.parseInt(month);  
    }  
	
	/**
	 * 获取当前时间 不要年月日 
	 * @param date
	 * @return
	 */
	public static String getTime(Date date){
		if (date != null) {
			return DateFormat.getTimeInstance().format(date);
		}
		return "";
		
	}
	
	  /**
     * 返回指定日期当前日 
     * @return
     */
	public static  int getDay(Date date)  
    {  
        String month = new SimpleDateFormat("dd").format(date);  
        return Integer.parseInt(month);  
    }  
  
    /**
     * 判断闰年  
     * @param year
     * @return
     */
	public static  boolean isLeap(int year)  
    {  
        if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))  
            return true;  
        else  
            return false;  
    }  
  
    /**
     * 返回当月天数  
     * @param year
     * @param month
     * @return
     */
	public static  int getDays(int year, int month)  
    {  
        int days;  
        int FebDay = 28;  
        if (isLeap(year))  
            FebDay = 29;  
        switch (month)  
        {  
            case 1:  
            case 3:  
            case 5:  
            case 7:  
            case 8:  
            case 10:  
            case 12:  
                days = 31;  
                break;  
            case 4:  
            case 6:  
            case 9:  
            case 11:  
                days = 30;  
                break;  
            case 2:  
                days = FebDay;  
                break;  
            default:  
                days = 0;  
                break;  
        }  
        return days;  
    }  
	
}